package cc.idoone.spring_boot_hello.web.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import javax.servlet.http.HttpSessionBindingEvent;

/**
 * <h3>WebListener 测试</h3>
 * <p>
 * 需要在启动入口处添加一个核心的注解@ServletComponentScan
 * </p>
 * <p>
 * 方式二：定义Bean: ServletListenerRegistrationBean(参考 ApplicationConfiguration)
 * </p>
 *
 * @author hemj
 * @version 2017/9/22
 */
@WebListener
public class HelloListener implements ServletContextListener,
		HttpSessionListener, HttpSessionAttributeListener {

	// Public constructor is required by servlet spec
	public HelloListener() {
	}

	// -------------------------------------------------------
	// ServletContextListener implementation
	// -------------------------------------------------------
	public void contextInitialized(ServletContextEvent sce) {
	  /* This method is called when the servlet context is
		 initialized(when the Web application is deployed).
         You can initialize servlet context related data here.
      */
		System.out.println("HelloListener contextInitialized");
	}

	public void contextDestroyed(ServletContextEvent sce) {
	  /* This method is invoked when the Servlet Context
		 (the Web application) is undeployed or
         Application Server shuts down.
      */
		System.out.println("HelloListener contextDestroyed");
	}

	// -------------------------------------------------------
	// HttpSessionListener implementation
	// -------------------------------------------------------
	public void sessionCreated(HttpSessionEvent se) {
	  /* Session is created. */
		System.out.println("HelloListener sessionCreated");
	}

	public void sessionDestroyed(HttpSessionEvent se) {
      /* Session is destroyed. */
		System.out.println("HelloListener sessionDestroyed");
	}

	// -------------------------------------------------------
	// HttpSessionAttributeListener implementation
	// -------------------------------------------------------

	public void attributeAdded(HttpSessionBindingEvent sbe) {
      /* This method is called when an attribute 
         is added to a session.
      */
	}

	public void attributeRemoved(HttpSessionBindingEvent sbe) {
      /* This method is called when an attribute
         is removed from a session.
      */
	}

	public void attributeReplaced(HttpSessionBindingEvent sbe) {
      /* This method is invoked when an attibute
         is replaced in a session.
      */
	}
}
